/**
 * 2017年7月17日
 */
package template;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateSequenceModel;

/**
 * 学习一种模板技术.
 * @author Alex
 *
 */

public class FreeMarkerTemplate {
	TemplateModel tm;
	TemplateHashModel thm;
	TemplateSequenceModel tsm;
	public static void main(String[] args) throws IOException, TemplateException {
		// Create your Configuration instance, and specify if up to what FreeMarker
		// version (here 2.3.25) do you want to apply the fixes that are not 100%
		// backward-compatible. See the Configuration JavaDoc for details.
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

		// Specify the source where the template files come from. Here I set a
		// plain directory for it, but non-file-system sources are possible too:
		cfg.setDirectoryForTemplateLoading(new File("D:\\template"));

		// Set the preferred charset template files are stored in. UTF-8 is
		// a good choice in most applications:
		cfg.setDefaultEncoding("UTF-8");

		// Sets how errors will appear.
		// During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

		// Don't log exceptions inside FreeMarker that it will thrown at you anyway:
		cfg.setLogTemplateExceptions(false);
		
		Template temp = cfg.getTemplate("weather.nominal.arff");
		Writer out = new OutputStreamWriter(System.out);
		Map<String, Object> m = Maps.newHashMap();
		m.put("frutes",Lists.asList("apple", new String[]{"bananas"}));
		m.put("beans",Lists.asList(new Bean("abc","def",1),new Bean[]{new Bean("ghi","jkl",2)}));
		m.put("end", "this is the end of the fiel");
		m.put("repeat", new RepeatDirective());
		temp.process(m, out);
		
		
		
		
	}
}
